package com.yihuan.controller;

import com.yihuan.entity.PlaintEntity;
import com.yihuan.global.MessageConstant;
import com.yihuan.service.PlaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/4/27 16:54
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class PlaintController {
    int pageNumber = 0;
    int count = 0;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    //数据页数和起始值
    private int num;
    @Autowired
    PlaintService plaintService;
    
    /**
     * 新增数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/insertPlaintByPlaintData")
    public Map<String, Object> insertPlaintByPlaintData(@RequestBody PlaintEntity model, HttpSession session) {
        // model.setUserID((Integer) session.getAttribute("UserID_4"));
        //设置时间的格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取时间的方法
        Date d = new Date();
        //将获取的时间转换成设置的时间格式进行存储
        model.setPlaintDate(sf.format(d));
        //状态
        model.setPlaintState(1);
        //公司编号
        model.setUserID((Integer) session.getAttribute("UserID_4"));
        int insertOrderByOrderData = plaintService.insertPlaintByPlaintData(model);
        if (insertOrderByOrderData == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.PLAINT_INSERT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.PLAINT_INSERT_SUCCESS);
        return map;
    }
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findPlaintByPlaintList")
    public Map<String, Object> findPlaintByPlaintList(@RequestBody PlaintEntity model, HttpSession session) {
        List<PlaintEntity> findPlaintByPlaintList = plaintService.findPlaintByPlaintList(model);
        if (findPlaintByPlaintList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.PLAINT_LIMIT_FALL);
            return map;
        }
        pageNumber = countPlaintList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("plaint_List", findPlaintByPlaintList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.PLAINT_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countPlaintList(PlaintEntity model) {
        count = plaintService.countPlaintList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findPlaintByPlaintList_1")
    public Map<String, Object> findPlaintByPlaintList_1(@RequestBody PlaintEntity model, HttpSession session) {
        model.setCompID((Integer) session.getAttribute("compID"));
        List<PlaintEntity> findPlaintByPlaintList = plaintService.findPlaintByPlaintList(model);
        if (findPlaintByPlaintList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.PLAINT_LIMIT_FALL);
            return map;
        }
        pageNumber = countPlaintList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("plaint_List", findPlaintByPlaintList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.PLAINT_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countPlaintList_1(PlaintEntity model) {
        count = plaintService.countPlaintList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 留言
     *
     * @return
     */
    @RequestMapping("/sumPlaintListByCompID")
    public Map<String, Object> sumPlaintListByCompID(HttpSession session) {
        PlaintEntity model = new PlaintEntity();
        model.setCompID((Integer) session.getAttribute("compID"));
        double sumOrderListByCompID = plaintService.sumPlaintListByCompID(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("sumPlaint", sumOrderListByCompID);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
    /**
     * 总体留言
     *
     * @return
     */
    @RequestMapping("/sumPlaintList")
    public Map<String, Object> sumPlaintList(HttpSession session) {
        PlaintEntity model = new PlaintEntity();
        double sumOrderList = plaintService.sumPlaintList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("sumPlaint", sumOrderList);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
}
